1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <sstream> #include <opencv2/opencv.hpp> #include <stdlib.h> #include <time.h> using namespace cv; using namespace std;
int main(int argc, char *argv[]) { Mat src=imread("/media/lyf/doc/蒙太奇/change.jpeg");
if(src.empty()) { return -1; } resize(src,src,Size(),0.5,0.5); int rows=src.rows,cols=src.cols; int much_row=rows/50,much_col=cols/50; much_col=much_col*50<cols?much_col+1:much_col; much_row=much_row*50<rows?much_row+1:much_row;
Mat over=Mat(much_row*50,much_col*50,CV_8UC3,Scalar(255,255,255)); srand(time(NULL)); Mat micPic,temp; for(int i=0;i<much_row;++i) {
for(int j=0;j<much_col;++j) { std::string filename="/media/lyf/doc/蒙太奇/m"; std::stringstream ss; ss<<rand()%22+1; filename+=ss.str()+".jpeg"; micPic=imread(filename,CV_LOAD_IMAGE_UNCHANGED); if(micPic.empty()) { cout<<"读取过程中出现错误"<<endl; return -1; } resize(micPic,micPic,Size(50,50)); temp=Mat(over,Rect(j*50,i*50,50,50)); micPic.copyTo(temp); micPic.release(); } } Mat src_32f,over_32f; src.convertTo(src_32f,CV_32FC3); over.convertTo(over_32f,CV_32FC3); for(int i = 0; i < rows; ++i) { for(int j = 0; j < cols; ++j) {
over_32f.at<Vec3f>(i, j)[0] = saturate_cast<float>(0.382*over_32f.at<Vec3f>(i, j)[0]+0.618*src_32f.at<Vec3f>(i, j)[0]); over_32f.at<Vec3f>(i, j)[1] = saturate_cast<float>(0.382*over_32f.at<Vec3f>(i, j)[1]+0.618*src_32f.at<Vec3f>(i, j)[1]); over_32f.at<Vec3f>(i, j)[2] = saturate_cast<float>(0.382*over_32f.at<Vec3f>(i, j)[2]+0.618*src_32f.at<Vec3f>(i, j)[2]); } } over_32f.convertTo(over,CV_8UC3); over=Mat (over,Rect(0,0,cols,rows)); imshow("原图",src); imshow("生成的",over); resize(over,over,Size(),1.5,1.5); imwrite("over.jpeg",over); waitKey(0); return 0; }
|